package com.ruoyi.factory.taskShow.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factory.common.domain.AjaxBean;
import com.ruoyi.factory.taskShow.domain.TaskShow;
import com.ruoyi.factory.taskShow.service.SysTaskShowService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * @ClassName TaskShowController
 * @Description TODO
 * @Author liujiaji
 * @Date 2022/6/20 10:26
 */
@RestController
@RequestMapping("/system/taskShow")
public class TaskShowController extends BaseController {

    @Autowired
    private SysTaskShowService taskShowService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:list')")
    @GetMapping("/list")
    public AjaxResult list(TaskShow taskShow)
    {
        startPage();
        List<TaskShow> list = taskShowService.selectTaskShowList(taskShow);
        return AjaxResult.success(list);
    }

    /**
     * @Description //TODO 甘特图数据列表
     * @Author liujiaji
     * @Date 2022/6/27 9:39
     * @Param [TaskShow]
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @PreAuthorize("@ss.hasPermi('system:taskShow:gtList')")
    @GetMapping("/gtList")
    public AjaxResult gtList(TaskShow taskShow)
    {
        startPage();
        List<TaskShow> list = taskShowService.selectTaskShowList(taskShow);
        for (int i = 0; i < list.size(); i++) {
            String title = list.get(i).getTitle();
            Date taskStartDate = list.get(i).getTaskStartDate();
            Date taskEndDate = list.get(i).getTaskEndDate();
            list.get(i).setName(title);
            list.get(i).setBeginDate(new Date(taskStartDate.getTime()));
            list.get(i).setEndDate(new Date(taskEndDate.getTime()));
//            list.get(i).setParentId("0");
//            list.get(i).setId(String.valueOf(i));
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskShow taskShow)
    {
        List<TaskShow> list = taskShowService.selectTaskShowList(taskShow);
        ExcelUtil<TaskShow> util = new ExcelUtil<TaskShow>(TaskShow.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:query')")
    @GetMapping(value = "/{taskShowId}")
    public AjaxResult getInfo(@PathVariable("taskShowId") String taskShowId)
    {
        TaskShow taskShow = taskShowService.selectTaskShowById(taskShowId);
        return AjaxResult.success(taskShow);
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskShow taskShow)
    {
        //时间校验,不能小于子项目时间不能大于父项目时间
        AjaxBean ajaxBean = taskShowService.dateDispos(taskShow);
        if(!ajaxBean.getStatus()){
            return AjaxResult.error(ajaxBean.getMsg());
        }
        return toAjax(taskShowService.insertTaskShow(taskShow));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskShow taskShow)
    {
        //时间校验,不能小于子项目时间不能大于父项目时间
        AjaxBean ajaxBean = taskShowService.dateDispos(taskShow);
        if(!ajaxBean.getStatus()){
            return AjaxResult.error(ajaxBean.getMsg());
        }
        return toAjax(taskShowService.updateTaskShow(taskShow));
    }

    /**
     * 删除【请填写功能名称】非批量删除
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeById/{id}")
    public AjaxResult removeById(@PathVariable String id)
    {
        if(taskShowService.hasChildById(id)){
            return AjaxResult.error("存在子项目,不允许删除");
        }
        return toAjax(taskShowService.deleteTaskShowById(id));
    }

    /**
     * 删除【请填写功能名称】批量删除
     */
    @PreAuthorize("@ss.hasPermi('system:taskShow:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(taskShowService.deleteTaskShowByIds(ids));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(TaskShow taskShow)
    {
        List<TaskShow> taskShows = taskShowService.selectTaskShowList(taskShow);
        return AjaxResult.success(taskShowService.buildTaskShowTreeSelect(taskShows));
    }

    /**
     * 加载对应角色任务展示列表树
     */
    @GetMapping(value = "/roleTaskShowTreeselect/{roleId}")
    public AjaxResult roleTaskShowTreeselect(@PathVariable("roleId") Long roleId)
    {
        List<TaskShow> taskShows = taskShowService.selectTaskShowList(new TaskShow());
        AjaxResult ajax = AjaxResult.success();
//        ajax.put("checkedKeys", taskShowService.selectMenuListByRoleId(roleId));
        ajax.put("taskShow", taskShowService.buildTaskShowTreeSelect(taskShows));
        return ajax;
    }


    public static void main(String[] args) {
        String csrcid = "测试1";
        String def13 = "测试2";
        String def14 = "测试3";
        String ftmsesql = "select * from FTM_FTDELIVER_H where SYNCBILLID = '"+ csrcid +"'";
        String ftmsesql2 = "update FTM_FTDELIVER_H set def13 = '"+ def13 +"' , def14 = '"+def14+"'"+"where SYNCBILLID ='"+csrcid+"'";

        System.out.println(ftmsesql);
        System.out.println(ftmsesql2);
    }
}
